package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.util.AttributeSet;

import com.tianhui.consignortwofei.R;

/**
 * @author fgs
 * @date 2020/10/12
 * @description 渐变文字
 **/
public class GradientsTextView extends androidx.appcompat.widget.AppCompatTextView {
    private int startColor;
    private int endColor;

    public GradientsTextView(Context context) {
        this(context, null);
    }

    public GradientsTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        try {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GradientsTextView);
            startColor = typedArray.getColor(R.styleable.GradientsTextView_GradientsStartColor, getResources().getColor(R.color.colorWhite));
//            endColor = typedArray.getColor(R.styleable.GradientsTextView_GradientsEndColor, getResources().getColor(R.color.colorBluePrimary));
            endColor = typedArray.getColor(R.styleable.GradientsTextView_GradientsEndColor, Color.parseColor("#81B2FC"));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void onLayout(boolean changed,
                            int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
//            getPaint().setShader(new LinearGradient(
//                    0, 0, 0, getHeight(),
//                    startColor, endColor,
//                    Shader.TileMode.CLAMP));
            getPaint().setShader(new LinearGradient(
                    0, 0, 0, getHeight(),
                    new int[]{startColor, endColor}, new float[]{0.4f, 0.6f},
                    Shader.TileMode.CLAMP));
        }
    }


}
